const { sequelize, DataTypes } = require('../config/database');

const ActivityRating = sequelize.define('ActivityRating', {
  id: {
    type: DataTypes.INTEGER,
    primaryKey: true,
    autoIncrement: true
  },
  
  rating: {
    type: DataTypes.TINYINT,
    allowNull: false,
    validate: {
      min: 1,
      max: 5
    }
  },
  
  comment: {
    type: DataTypes.TEXT,
    allowNull: true
  }
  
}, {
  tableName: 'parkpart_activity_ratings',  // 修改表名
  timestamps: true,
  paranoid: true,
  indexes: [
    { fields: ['activityId'] },
    { fields: ['userId'] },
    { unique: true, fields: ['activityId', 'userId'] }
  ]
});

// 关联表定义
ActivityRating.associate = (models) => {
  // 评分所属活动
  ActivityRating.belongsTo(models.Activity, {
    foreignKey: 'activityId',
    as: 'activity'
  });
  
  // 评分用户
  ActivityRating.belongsTo(models.User, {
    foreignKey: 'userId',
    as: 'user'
  });
};

module.exports = ActivityRating;